package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.CommissionRankAdapter;
import com.niji.littly_donkey_android.adapter.MyHomeVideoAdapter;
import com.niji.littly_donkey_android.bean.CommissionRank;
import com.niji.littly_donkey_android.bean.Video;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.BitMapUtil;
import com.niji.littly_donkey_android.util.CirclePhotoUtil;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommissionRankActivity extends BaseActivity {
    private static final String TAG = "CommissionRankActivity";

    private Button commission_rank_return;

    private Button commission_rank_withdrawal;

    private TextView my_video;

    private TextView rank_details;

    private TextView commission_rank_bank;

    private ListView commission_rank_list;

    private ArrayList<CommissionRank> commissionRankArrayList;

    private LinearLayout commission_rank_list1;

    private RecyclerView commission_video_list;

    private TextView commission_video_text;

    private TextView commission_rank_text;

    private ImageView commission_rank_img;

    private ImageView commission_video_img;

    private TextView commission_rank_money;

    private RelativeLayout commission_rank_loading;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commission_rank_activity);
        init();
        commissionRankReturn();
        commissionRankWithdrawal();
        rankDetails();
        rankBank();
        checkPage();
        showRank();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.commission_rank_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }


    /**
     * 展示我的视频
     */
    private void showVideo() {
        commission_rank_loading.setVisibility(View.VISIBLE);
        showVideos();
        commission_rank_list1.setVisibility(View.INVISIBLE);
        commission_video_list.setVisibility(View.VISIBLE);
    }

    /**
     * 展示视频排行
     */
    private void showRank() {
        commission_rank_loading.setVisibility(View.VISIBLE);
        showRankVideos();
        commission_rank_list1.setVisibility(View.VISIBLE);
        commission_video_list.setVisibility(View.INVISIBLE);
    }


    /**
     * 获取视频排行
     */
    private void showRankVideos(){
        commission_rank_loading = findViewById(R.id.commission_rank_loading);
        commissionRankArrayList = new ArrayList<>();
        String url = UrlUtil.URL + "/video/ranking?page=1&per_page=100";
        RequestQueue requestQueue= Volley.newRequestQueue(CommissionRankActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map  = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0){
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Integer id = (Integer) mapList.get(i).get("id");
                            Integer user_id = (Integer) mapList.get(i).get("user_id");
                            Integer attractions_id = (Integer) mapList.get(i).get("attractions_id");
                            String title = (String) mapList.get(i).get("title");
                            String url1 = (String) mapList.get(i).get("url");
                            String cover = (String) mapList.get(i).get("cover");
                            Map<String,String> userInfo = (Map<String, String>) mapList.get(i).get("userInfo");
                            Integer likes = (Integer) mapList.get(i).get("likes");
                            Integer comments = (Integer) mapList.get(i).get("comments");
                            Integer commission = (Integer) mapList.get(i).get("commission");
                            String remark = (String) mapList.get(i).get("remark");
                            String nickname = userInfo.get("nickname");
                            String avatar = userInfo.get("avatar");
                            CommissionRank commissionRank = new CommissionRank();
                            commissionRank.setTitle(title);
                            commissionRank.setName(nickname);
                            commissionRank.setImg(UrlUtil.URL + cover);
                            commissionRank.setNumber((i + 1) + ".");
                            commissionRankArrayList.add(commissionRank);
                            commission_rank_loading.setVisibility(View.INVISIBLE);

                        }
                        commission_rank_list.setAdapter(new CommissionRankAdapter(CommissionRankActivity.this,commissionRankArrayList));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(CommissionRankActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(CommissionRankActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);
    }

    /**
     * 我的作品
     */
    private void showVideos() {
        ArrayList<Video> videos = new ArrayList<>();
        String url = UrlUtil.URL + "/user/videos/list?page=1&per_page=50";
        RequestQueue requestQueue= Volley.newRequestQueue(CommissionRankActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map  = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0){
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Integer id = (Integer) mapList.get(i).get("id");
                            Integer user_id = (Integer) mapList.get(i).get("user_id");
                            Integer attractions_id = (Integer) mapList.get(i).get("attractions_id");
                            String title = (String) mapList.get(i).get("title");
                            String url1 = (String) mapList.get(i).get("url");
                            String cover = (String) mapList.get(i).get("cover");
                            Integer likes = (Integer) mapList.get(i).get("likes");
                            Integer comments = (Integer) mapList.get(i).get("comments");
                            Integer commission = (Integer) mapList.get(i).get("commission");
                            String remark = (String) mapList.get(i).get("remark");
                            Video video = new Video();
                            video.setId(id);
                            video.setLikes(likes);
                            video.setAttractionsId(attractions_id);
                            video.setComments(comments);
                            video.setCommission(commission);
                            video.setTitle(title);
                            video.setUrl(UrlUtil.URL + cover);
                            video.setUserId(user_id);
                            videos.add(video);
                            commission_rank_loading.setVisibility(View.INVISIBLE);
                        }
                        commission_video_list.setHasFixedSize(true);
                        commission_video_list.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
                        commission_video_list.setAdapter(new MyHomeVideoAdapter(CommissionRankActivity.this,videos));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(CommissionRankActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(CommissionRankActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);


    }
    /***
     * 视频排行
     */
    private void checkPage() {
        commission_rank_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commission_rank_text.setTextColor(Color.parseColor("#333333"));
                commission_video_text.setTextColor(Color.parseColor("#999999"));
                commission_rank_img.setVisibility(View.VISIBLE);
                commission_video_img.setVisibility(View.INVISIBLE);
                commission_video_list.setVisibility(View.INVISIBLE);
                commission_rank_list1.setVisibility(View.VISIBLE);
                showRank();
            }
        });

        commission_video_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commission_rank_text.setTextColor(Color.parseColor("#999999"));
                commission_video_text.setTextColor(Color.parseColor("#333333"));
                commission_rank_img.setVisibility(View.INVISIBLE);
                commission_video_img.setVisibility(View.VISIBLE);
                commission_rank_list1.setVisibility(View.INVISIBLE);
                commission_video_list.setVisibility(View.VISIBLE);
                showVideo();
            }
        });

    }

//    @Override
//    protected void onStart() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            //不加这个clearFlags好像就没效果
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            window.getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            //设置导航栏透明(就是虚拟键那一栏)
//
//            //设置状态栏(或者叫通知栏)透明
//
//            window.setStatusBarColor(Color.TRANSPARENT);
//        }
//        super.onStart();
//    }

    /**
     * 初始化。获取佣金金额
     */
    private void init() {
        commission_rank_loading = findViewById(R.id.commission_rank_loading);
        commission_rank_list = findViewById(R.id.commission_rank_list);
        commission_rank_list1 = findViewById(R.id.commission_rank_list1);
        commission_video_list = findViewById(R.id.commission_video_list);
        commission_video_text = findViewById(R.id.commission_video_text);
        commission_rank_text = findViewById(R.id.commission_rank_text);
        commission_rank_img = findViewById(R.id.commission_rank_img);
        commission_video_img = findViewById(R.id.commission_video_img);
        commission_rank_money = findViewById(R.id.commission_rank_money);
        String url = UrlUtil.URL + "/commission/my/commission";
        RequestQueue requestQueue= Volley.newRequestQueue(CommissionRankActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        Double commission = (Double) dataMap.get("commission");
                        commission_rank_money.setText("￥" + commission);
                    } else if(code == -1){
                        Toast.makeText(CommissionRankActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(CommissionRankActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(CommissionRankActivity.this, "佣金获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(CommissionRankActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    /**
     * 返回
     */
    private void commissionRankReturn() {
        commission_rank_return = findViewById(R.id.commission_rank_return);
        commission_rank_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });
    }

    /**
     * 提现
     */
    private void commissionRankWithdrawal() {
        commission_rank_money = findViewById(R.id.commission_rank_money);
        commission_rank_withdrawal = findViewById(R.id.commission_rank_withdrawal);
        commission_rank_withdrawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String commissionRankMoney = commission_rank_money.getText().toString().trim();
                Intent intent = new Intent(CommissionRankActivity.this, BillBillActivity.class);
                intent.putExtra("commissionRankMoney",commissionRankMoney);
                startActivity(intent);
                   
            }
        });
    }

    /**
     * 明细
     */

    private void rankDetails() {
        rank_details = findViewById(R.id.rank_details);
        rank_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CommissionRankActivity.this, BillDetailsActivity.class);
                intent.putExtra("mode","details");
                startActivity(intent);
                   
            }
        });
    }

    private void rankBank() {
        commission_rank_bank = findViewById(R.id.commission_rank_bank);
        commission_rank_bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CommissionRankActivity.this,BillBankCardActivity.class));
                   
            }
        });
    }

}
